﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;

public partial class Admin_Nhom_san_pham : System.Web.UI.Page
{
    TblGroupDAO groupDAO = new TblGroupDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadGroup();
            string id = Request.QueryString["id"];
            if (id != null || id != "")
            {
                GroupDetail();
            }
        }
    }

    // load chi tiết nhóm
    private void GroupDetail()
    {
        string id = Request.QueryString["id"];
       // TblGroup group = new TblGroup();
        DataTable dt = groupDAO.GetGroup_ByID(Global.ConvertToInt(id));
        foreach (DataRow dr in dt.Rows)
        {
            txtGroupName.Text = dr["GroupName"].ToString();
            drLang.SelectedValue =  Convert.ToInt32 (dr["IdLang"]).ToString();
        }
    }

    // load Gridview
    private void LoadGroup()
    {
        grvGroup.DataSource = groupDAO.GetAll();
        grvGroup.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            TblGroup group = new TblGroup();
            group.GroupName = txtGroupName.Text;
            group.IdLang = Convert.ToInt32(drLang.SelectedValue);

            if (id == null || id == "")
            {
                groupDAO.InsUp(group, 1);
            }
            else
            {
                group.IDGroup = Global.ConvertToInt(id);
                groupDAO.InsUp(group, 2);
            }
            flag = true;

        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            Response.Redirect("~/Admin/Nhom-san-pham.aspx");
        }
    }
}
